<template>
  <el-card shadow="hover" class="mt-20">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddSpu"
    >
      添加SPU
    </el-button>

    <el-table :data="spuList" border class="mt-20" v-loading="loading">
      <el-table-column type="index" align="center" label="序号" width="60" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            size="small"
            title="添加SKU"
            @click="showAddSku(row)"
          />
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            title="修改SPU"
            @click="showUpdateSpu(row)"
          />
          <el-button
            type="info"
            :icon="InfoFilled"
            size="small"
            title="查看SKU列表"
            @click="showSkuList(row.id, row.spuName)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            size="small"
            title="删除SPU"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jump, ->, sizes, total"
      @current-change="getSpuList"
      @size-change="getSpuList"
    />

    <el-dialog :title="`${spuName}的SKU列表`" v-model="dialogVisible">
      <el-table :data="skuList" border max-height="300" v-loading="skuLoading">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格（元）" prop="price" />
        <el-table-column label="重量（KG）" prop="weight" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img :src="row.skuDefaultImg" class="sku__img" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { Delete, Edit, InfoFilled, Plus } from "@element-plus/icons-vue";
import { ref, watch } from "vue";
import { getSpuListApi } from "@/api/product/spu";
import { getSkuListBySpuIdApi } from "@/api/product/sku";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import type { SkuList } from "@/api/product/model/skuModel";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";

// 显示sku列表
const dialogVisible = ref(false);
const skuList = ref<SkuList>([]);
const spuName = ref("");
const skuLoading = ref(false);

const showSkuList = async (spuId: number, name: string) => {
  skuLoading.value = true;
  dialogVisible.value = true;
  spuName.value = name;
  skuList.value = await getSkuListBySpuIdApi(spuId);
  skuLoading.value = false;
};

// spu列表分页展示
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const spuList = ref<SpuList>([]);
const loading = ref(false);

const categoryStore = useCategoryStore();

const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      total.value = 0;
      currentPage.value = 1;
      pageSize.value = 3;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);

const emit = defineEmits(["setIsShow"]);

const spuStore = useSpuStore();

// 显示添加SPU
const showAddSpu = () => {
  emit("setIsShow", 2);
  spuStore.$reset();
};

// 显示修改SPU
const showUpdateSpu = (row: SpuItem) => {
  emit("setIsShow", 2);
  spuStore.spu = row;
};

// 显示添加SKU
const showAddSku = (row: SpuItem) => {
  emit("setIsShow", 3);
  spuStore.spu = row;
};
</script>

<style scoped>
.sku__img {
  width: 100px;
  height: 100px;
}
</style>
